var cells = document.querySelectorAll('.cell');
var w = document.querySelector('.w');
var pp = document.querySelector('.pp');
var btn = document.querySelector('.restart');
var step = 0;
var now = 'x';
btn.addEventListener('click',function(){
		w.style.display = 'none';
		for (let i = 0; i < cells.length; i++) {
				cells[i].classList.remove('x','o');
				cells[i].removeAttribute('data-yes');
				step = 0;
				now = 'x';
		}
})
for (let i = 0; i < cells.length; i++) {
		cells[i].addEventListener('mouseover', function () {
				if (!this.getAttribute('data-yes')) {
						this.classList.add(now);
				}
		})
		cells[i].addEventListener('mouseout', function () {
				if (!this.getAttribute('data-yes')) {
						this.classList.remove(now);
				}
		})
		cells[i].addEventListener('click', function () {
				if (!this.getAttribute('data-yes')) {
						this.classList.add(now);
						this.setAttribute('data-yes', true);
						step++;
						if (checkWin(cells, now)) {
								pp.innerHTML = now + ' 赢了';
								w.style.display = 'block';
								return;
						}
						if (step == 9) {
								pp.innerHTML = '平局';
								w.style.display = 'block';
								return;
						}
						now = now == 'o' ? 'x' : 'o';
				}
		})
}
function checkWin(obj, now) {
		var flag = true;
		var winarr = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [2, 4, 6]];
		for (var i = 0; i < winarr.length; i++) {
				flag = true;
				for (var j = 0; j < winarr[i].length; j++) {
						if (Array.from(obj[winarr[i][j]].classList).indexOf(now) == -1) {
								flag = false;
						}
				}
				if (flag == true) {
						break;
				}
		}
		return flag;
}